﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace DocManagerORM.Utils
{
   public class Win32WMI_Service
    {               
        public string Name { get; set; }
        public string Caption { get; set; }
        public string DisplayName { get; set; }

        public string PathName { get; set; }
        public string ServiceType { get; set; }
        public string StartName { get; set; }
        public string StartMode { get; set; }

        public string State { get; set; }
        public string Status { get; set; }
        public int ProcessId { get; set; }

       public bool Started { get; set; }

        
        public int CheckPoint { get; set; }
        public int ExitCode { get; set; }
        public int TagId { get; set; }
        public int WaitHint { get; set; }
        public int ServiceSpecificExitCode { get; set; }        
        
        public string CreationClassName { get; set; }
        
        public string ErrorControl { get; set; }
        public string Description { get; set; }
       
       public string SystemCreationClassName { get; set; }

       public bool AcceptPause { get; set; }
       public bool AcceptStop { get; set; }
       public bool DesktopInteract { get; set; }

       public string SystemName { get; set; }
               
        public DateTime InstallDate;             
        
        public static string[] getCampiNames()
        {
            return new string[]{
           "bool AcceptPause",
           "bool AcceptStop",
           "string Caption",
           "int CheckPoint",
           "string CreationClassName",
           "string Description",
           "bool DesktopInteract",
           "string DisplayName",
           "string ErrorControl",
           "int ExitCode",
           "DateTime InstallDate",
           "string Name",
           "string PathName",
           "int ProcessId",
           "int ServiceSpecificExitCode",
           "string ServiceType",
           "bool Started",
           "string StartMode",
           "string StartName",
           "string State",
           "string Status",
           "string SystemCreationClassName",
           "string SystemName",
           "int TagId",
           "int WaitHint" 
           };
        }//fM

        public static IList<Win32WMI_Service> enumeraIstanzeServiziViaWMI(string hostName, string pNameServiceContain)
        {
            var sb = new StringBuilder();
            string[] ret = null;

            IList<Win32WMI_Service> listRet = new List<Win32WMI_Service>();

            pNameServiceContain = pNameServiceContain.Trim().ToLower();

            try
            {
                var path = @"\\" + hostName + @"\root\cimv2";

                ManagementScope ms = new ManagementScope(path);
                ms.Connect();
                //var sqlServerServiceName = "sqlservr";


                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                    "SELECT * FROM Win32_Service WHERE Started = TRUE "))
                {
                    //AND DisplayName='" + sqlServerServiceName + "'"

                    searcher.Scope = ms;

                    string servizioName = "";

                    Win32WMI_Service dati = null;
                    
                    bool procedi = false;

                    bool vuota = string.IsNullOrEmpty(pNameServiceContain);

                    if (searcher != null && searcher.Get() != null)
                    {
                        foreach (ManagementObject service in searcher.Get())
                        {
                            servizioName = service["Caption"].ToString().ToLower();

                            if (vuota)
                                procedi = true;
                            else
                                procedi = servizioName.Contains(pNameServiceContain);
                            
                            if(procedi)                            
                             {
                                 try
                                 {
                                     dati = new Win32WMI_Service()
                                     {
                                         Description = service["Description"].ToString(),
                                         Name = service["Name"].ToString(),
                                         DisplayName = service["DisplayName"].ToString(),
                                         Caption = service["Caption"].ToString(),
                                         ProcessId = int.Parse(service["ProcessId"].ToString()),
                                         PathName = service["PathName"].ToString(),
                                         Status = service["Status"].ToString(),
                                         State = service["State"].ToString(),
                                         Started = bool.Parse(service["Started"].ToString()),
                                         SystemName = service["SystemName"].ToString(),
                                         StartName = service["StartName"].ToString()
                                     };

                                     try
                                     {
                                         dati.AcceptPause = bool.Parse(service["AcceptPause"].ToString());
                                         dati.AcceptStop = bool.Parse(service["AcceptPause"].ToString());
                                         dati.CheckPoint = int.Parse(service["CheckPoint"].ToString());
                                         dati.ErrorControl = service["ErrorControl"].ToString();
                                         dati.ExitCode = int.Parse(service["ExitCode"].ToString());
                                         dati.ServiceSpecificExitCode = int.Parse(service["ServiceSpecificExitCode"].ToString());
                                         dati.WaitHint = int.Parse(service["WaitHint"].ToString());
                                         dati.SystemCreationClassName = service["SystemCreationClassName"].ToString();
                                     }
                                     catch { }

                                     try
                                     {
                                         dati.CreationClassName = service["CreationClassName"].ToString();
                                         dati.DesktopInteract = bool.Parse(service["DesktopInteract"].ToString());
                                         ////InstallDate = DateTime.Parse(service["InstallDate"].ToString());
                                         dati.ServiceType = service["ServiceType"].ToString();
                                         dati.TagId = int.Parse(service["TagId"].ToString());
                                         dati.StartMode = service["StartMode"].ToString();
                                     }
                                     catch { }

                                     sb.Append(hostName + ". " + servizioName + " ( RUNNING \r\n");
                                     listRet.Add(dati); 
                                 }
                                 catch(Exception) 
                                 { 
                                    
                                 }                                
                             }
                        }
                    }
                }

                ret = sb.ToString().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                sb.Clear();

                return listRet;

            }
            catch (Exception e)
            {
                sb.Append(hostName + " service - UNVERIFIABLE\n");
            }

            return listRet;

        }


    };
}
